//
//  YHMeAddAddressViewController.m
//  YHApp
//
//  Created by -- on 2021/6/19.
//

#import "HAAddAddressVC.h"
#import "HAAddressMo.h"
#import "HAChooseTabPopView.h"
#import "HAAddressPopView.h"

@interface HAAddAddressVC ()<UITextFieldDelegate>

/***/
@property (nonatomic, strong) UITextField *textField1;
/***/
@property (nonatomic, strong) UITextField *textField2;
/***/
@property (nonatomic, strong) UITextField *textField4;
/***/
@property (nonatomic, strong) UILabel *addrLabel;
/***/
@property (nonatomic, strong) FL_Button *isDButton;

/***/
@property (nonatomic, strong) NSString *is_default;

/***/
@property (nonatomic, copy) YHMeAddAddrReturnBlock block;

@end

@implementation HAAddAddressVC


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"新增收货地址";
    if (self.model.is_default == 1) {
        self.is_default = @"1";
    }else {
        self.is_default = @"0";
    }
    
    [self setupUI];
    
    // Do any additional setup after loading the view.
}

-(void)returnBlock:(YHMeAddAddrReturnBlock)block {
    _block = block;
}

-(void)mitButtonClick {
    
    [self.view endEditing:YES];
    
    if ([NSString isEmptyString:self.textField1.text]) {
        [MBProgressHUD showError:@"请输入收件人姓名"];
        return;
    }
    if ([NSString isEmptyString:self.textField2.text]) {
        [MBProgressHUD showError:@"请输入联系人电话号码"];
        return;
    }
    if ([NSString isEmptyString:self.addrLabel.text] || [self.addrLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD showError:@"请选择地址"];
        return;
    }
    if ([NSString isEmptyString:self.textField4.text]) {
        [MBProgressHUD showError:@"请输入街道地址"];
        return;
    }

    NSDictionary *dict = @{
        @"ids":self.model.ID?:@"0",
        @"surname":self.textField1.text,
        @"mobile":self.textField2.text,
        @"region":self.addrLabel.text,
        @"address":self.textField4.text,
        @"is_default":self.is_default,
    };

    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodPOST;
    request.requestUrl = @"address/edit";
    request.parms = dict;
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
                if (self.block) {
                    self.block();
                }
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

-(void)isDButtonClick {
    [self.view endEditing:YES];
    WS(weakSelf)
    NSMutableArray *dicts = [NSMutableArray array];
    [dicts addObject:@{@"color":@"#373737",@"title":@"是"}];
    [dicts addObject:@{@"color":@"#373737",@"title":@"否"}];
    
    HAChooseTabPopView *menu = [[HAChooseTabPopView alloc]initWithDicts:dicts title:@"是否默认"];
    [menu show];
    menu.YHChooseTabMenuCellBlock = ^(NSString * _Nonnull title) {
        [weakSelf.isDButton setTitle:title forState:UIControlStateNormal];
        if ([title isEqualToString:@"是"]) {
            weakSelf.is_default = @"1";
        }else {
            weakSelf.is_default = @"0";
        }
    };
}

-(void)addrClick {
    
    [self.view endEditing:YES];

    WS(weakSelf)
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"region/lists.html";
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                NSArray *arr = [HAAddressProvinceMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                
                HAAddressPopView *menu = [[HAAddressPopView alloc]initWithDZArray:arr];
                [menu show];
                menu.HAAddressPopViewSureBlock = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull area) {
                    weakSelf.addrLabel.text = [NSString stringWithFormat:@"%@/%@/%@",province,city,area];
                };
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}


-(void)setupUI {
    
    UIView *bgView = [UIView ha_createView];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_offset(325);
    }];
    
    for (int i = 0; i < 5; i ++) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor kLineColor];
        [bgView addSubview:lineView];
        CGFloat lineViewY = 0;
        if (i == 0) {
            lineViewY = 30+53.4;
        }else {
            lineViewY = 30 + 53.4 + i * 53.4;
        }
        lineView.frame = CGRectMake(20, lineViewY, kUIScreen_w-40, 0.6);
    }
    
    NSMutableArray *nameArray = [NSMutableArray array];
    [nameArray addObject:@"姓名"];
    [nameArray addObject:@"联系电话"];
    [nameArray addObject:@"省市区/县"];
    [nameArray addObject:@"街道地址"];
    [nameArray addObject:@"是否默认"];
    
    for (int i = 0; i < nameArray.count; i ++) {
        NSString *name = nameArray[i];
        UILabel *nameLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:name alignment:0];
        [bgView addSubview:nameLabel];
        CGFloat labelY = 0;
        if (i == 0) {
            labelY = 30+18;
        }else {
            labelY = 30 + i * 53.4 + 18;
        }
        nameLabel.frame = CGRectMake(20, labelY, 88, 18);
    }
    
    UITextField *textField1 = [[UITextField alloc]init];
    self.textField1 = textField1;
    textField1.delegate = self;
    textField1.textColor = [UIColor colorWithHexString:@"#333333"];
    textField1.font = [UIFont systemFontOfSize:15];
    textField1.placeholder = @"收货人姓名";
    textField1.text = self.model.surname;
    textField1.textAlignment = 2;
    [bgView addSubview:textField1];
    [textField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(30);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(100);
        make.top.mas_equalTo(30+10);
    }];
    
    UITextField *textField2 = [[UITextField alloc]init];
    self.textField2 = textField2;
    textField2.delegate = self;
    textField2.textColor = [UIColor colorWithHexString:@"#333333"];
    textField2.font = [UIFont systemFontOfSize:15];
    textField2.placeholder = @"收货人联系电话";
    textField2.text = self.model.mobile;
    textField2.textAlignment = 2;
    [bgView addSubview:textField2];
    [textField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(30);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(100);
        make.top.mas_equalTo(30+54+10);
    }];
    
    UILabel *addrLabel = [UILabel ha_NLabelWithFont:13 textColor:@"#333333" text:@"" alignment:2];
    addrLabel.userInteractionEnabled = YES;
    [addrLabel addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addrClick)]];
    self.addrLabel = addrLabel;
    [bgView addSubview:addrLabel];
    [addrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(30);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(100);
        make.top.mas_equalTo(30+54+54+10);
    }];
    
    if (self.model) {
        addrLabel.text = self.model.region;
    }else {
        addrLabel.text = @"请选择";
    }
    
    UITextField *textField4 = [[UITextField alloc]init];
    self.textField4 = textField4;
    textField4.delegate = self;
    textField4.textColor = [UIColor colorWithHexString:@"#333333"];
    textField4.font = [UIFont systemFontOfSize:15];
    textField4.placeholder = @"收货人街道地址";
    textField4.text = self.model.address;
    textField4.textAlignment = 2;
    [bgView addSubview:textField4];
    [textField4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(30);
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(100);
        make.top.mas_equalTo(30+54+54+54+10);
    }];
    
    NSString *isDtext = @"";
    if (self.model.is_default == 1) {
        isDtext = @"是";
    }else {
        isDtext = @"否";
    }
    FL_Button *isDButton = [[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusRight];
    self.isDButton = isDButton;
    [isDButton setTitle:isDtext forState:UIControlStateNormal];
    [isDButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    isDButton.titleLabel.font = [UIFont systemFontOfSize:12];
    isDButton.fl_padding = 6;
    [isDButton setImage:[UIImage imageNamed:@"ic_cell_more"] forState:UIControlStateNormal];
    [isDButton addTarget:self action:@selector(isDButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:isDButton];
    [isDButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(68);
        make.height.mas_offset(30);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(30+54+54+54+54+10);
    }];
    
    
    UIButton *mitButton = [[UIButton alloc]init];
    [mitButton setTitle:@"提交" forState:UIControlStateNormal];
    [mitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    mitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    mitButton.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    mitButton.layer.cornerRadius = 4;
    mitButton.layer.masksToBounds = true;
    [mitButton addTarget:self action:@selector(mitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mitButton];
    [mitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.top.mas_equalTo(bgView.mas_bottom).offset(30);
        make.height.mas_offset(44);
    }];
    
}


@end
